package com.atgui.yygh.controller;

import com.atgui.commom.utils.MD5;
import com.atgui.yygh.service.HospitalSetService;
import com.atgui.yygh.model.hosp.HospitalSet;
import com.atgui.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Author:wzh
 * @Date 2022 08 05 17 47
 **/
@Api("医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin   //跨域访问
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("查询所有医院信息")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> FindHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    @ApiOperation("分页查询结果")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page=new Page<>(current,limit);
        QueryWrapper<HospitalSet> queryWrapper=new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        queryWrapper.like(hosname!=null,"hosname",hospitalSetQueryVo.getHosname());
        queryWrapper.eq(hoscode!=null,"hoscode",hospitalSetQueryVo.getHoscode());
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }
    @ApiOperation("逻辑删除")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("保存结果")
    @PostMapping("saveHospitalSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        return flag ? Result.ok():Result.fail();
    }
    @ApiOperation("根据id查询")
    @GetMapping("/getHospSet/{id}")
    public Result GetHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    @ApiOperation("更新操作")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok(): Result.fail();
    }
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        return flag ? Result.ok(): Result.fail();
    }
    @ApiOperation("锁定医院")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.save(hospitalSet);
        return Result.ok();
    }
    @ApiOperation("发送密钥")
    @PutMapping("/lockHospitalSet/{id}")
    public Result lockHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        return Result.ok();
    }
}

